﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.ComponentModel;

namespace DAO
{
   public class EmployeeDAO
    {
        public EmployeeDTO CreateEmployeeDTOFromEntity(NhanVien nhanVien)
        {
            EmployeeDTO EmpDto = new EmployeeDTO
            {
                EmployeeID = nhanVien.NhanVienID,
                EmployeeCode = nhanVien.MaNhanVien ?? string.Empty,
                EmployeeName = nhanVien.HoTen ?? string.Empty,
                EmployeeBirthDay = (DateTime)nhanVien.NgaySinh,
                EmployeeEmail = nhanVien.Email ?? string.Empty,
                EmployeePhone = nhanVien.DienThoai ?? string.Empty,
                EmployeeCMND = nhanVien.CMND ?? string.Empty,
                EmployeeSex =  bool.Parse(nhanVien.GioiTinh.ToString()),
                EmployeeSexName = bool.Parse(nhanVien.GioiTinh.ToString())?"Nam" : "Nữ" ,
                EmployeeAddress = nhanVien.DiaChi ?? string.Empty,
                EmployeeBeginDay = nhanVien.NgayThem.HasValue ? nhanVien.NgaySinh.Value : DateTime.Now,
                EmployeeKindCode = (int)nhanVien.MaLoaiNV,
                EmployeeNote = nhanVien.GhiChu ?? string.Empty,
                EmployeeStatus = bool.Parse(nhanVien.SuDung.ToString())

            };
            return EmpDto;
        }
        public int InsertEmployee(EmployeeDTO employeeDto)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    NhanVien nhanVienInt = new NhanVien
                    {
                        MaNhanVien = employeeDto.EmployeeCode,
                        HoTen = employeeDto.EmployeeName,
                        NgaySinh = employeeDto.EmployeeBirthDay,
                        Email = employeeDto.EmployeeEmail,
                        DienThoai = employeeDto.EmployeePhone,
                        CMND = employeeDto.EmployeeCMND,
                        GioiTinh = employeeDto.EmployeeSex,
                        DiaChi = employeeDto.EmployeeAddress,
                        NgayThem = employeeDto.EmployeeBeginDay,
                        MaLoaiNV = employeeDto.EmployeeKindCode,
                        GhiChu = employeeDto.EmployeeNote,
                        SuDung = true
                    };

                    if (ent.NhanViens.Count(t => t.NhanVienID != null) > 0)
                    {
                        nhanVienInt.NhanVienID = ent.NhanViens.Max(t => t.NhanVienID) + 1;
                    }
                    else
                    {
                        nhanVienInt.NhanVienID = 1;
                    }

                    ent.AddToNhanViens(nhanVienInt);

                    DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                    docSeqDAO.UpdateNextDocSequence(DocSequence.Employee.ToString());

                    ent.SaveChanges();

                    return nhanVienInt.NhanVienID;
                }
            }
            catch
            {
                return -1;
            }
          
        }
        public List<EmployeeDTO> GetEmployeeList()
        {
            List<EmployeeDTO> listEmployeeDto = new List<EmployeeDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<NhanVien> listNhanVien = ent.NhanViens.Where(c => c.SuDung == true).ToList();
                foreach (NhanVien item in listNhanVien)
                {
                    EmployeeDTO empDto = CreateEmployeeDTOFromEntity(item);
                    listEmployeeDto.Add(empDto);
                }
            }
            return listEmployeeDto;
        }

        public BindingList<EmployeeDTO> GetNewBindingListEmployee()
        {
            BindingList<EmployeeDTO> listEmployeeDto = new BindingList<EmployeeDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<NhanVien> listNhanVien = ent.NhanViens.Where(c => c.SuDung == true).ToList();
                foreach (NhanVien item in listNhanVien)
                {
                    EmployeeDTO EmpDto = CreateEmployeeDTOFromEntity(item);
                    listEmployeeDto.Add(EmpDto);
                }
            }
            return listEmployeeDto;
        }

        public bool UpdateEmployee(EmployeeDTO EmpDTO)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    NhanVien EmpEnt = ent.NhanViens.FirstOrDefault(c => c.NhanVienID == EmpDTO.EmployeeID);

                    EmpEnt.HoTen = EmpDTO.EmployeeName;
                    EmpEnt.NgaySinh = EmpDTO.EmployeeBirthDay;
                    EmpEnt.Email = EmpDTO.EmployeeEmail;
                    EmpEnt.DienThoai = EmpDTO.EmployeePhone;
                    EmpEnt.CMND = EmpDTO.EmployeeCMND;
                    EmpEnt.GioiTinh = EmpDTO.EmployeeSex;
                    EmpEnt.DiaChi = EmpDTO.EmployeeAddress;
                    EmpEnt.NgayThem = EmpDTO.EmployeeBeginDay;
                    EmpEnt.MaLoaiNV = EmpDTO.EmployeeKindCode;
                    EmpEnt.SuDung = EmpDTO.EmployeeStatus;
                    EmpEnt.GhiChu = EmpDTO.EmployeeNote;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public EmployeeDTO GetEmployeeByID(int EmpID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    NhanVien emp = ent.NhanViens.FirstOrDefault(c => c.NhanVienID == EmpID);

                    EmployeeDTO EmpDTO = CreateEmployeeDTOFromEntity(emp);

                    return EmpDTO;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool DeleteEmployee(int empID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    NhanVien EmpEnt = ent.NhanViens.FirstOrDefault(c => c.NhanVienID == empID);
                    EmpEnt.SuDung = false;
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckExistsCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.NhanViens.Where(c => c.MaNhanVien == code).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return true;
            }
        }
        public List<UsersGroupDTO> Load_UserGroup()
        {
            List<UsersGroupDTO> listUsersGroupDto = new List<UsersGroupDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<LoaiNhanVien> listLoaiNhanVien = ent.LoaiNhanViens.Where(c => c.SuDung == true).ToList();
                foreach (LoaiNhanVien item in listLoaiNhanVien)
                {
                    UsersGroupDTO USGDto = new UsersGroupDTO
                    {
                        UsersGroupID = item.LoaiNhanVienID,
                        UsersGroupCode = item.MaLoaiNhanVien,
                        UsersGroupName = item.TenLoaiNhanVien,
                        Status = (bool)item.SuDung
                    };

                    listUsersGroupDto.Add(USGDto);
                }
            }
            return listUsersGroupDto;
        }

        public List<EmployeeDTO> GetEmployeeNotHasAccount()
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    List<EmployeeDTO> ListEmployee = new List<EmployeeDTO>();
                    List<NhanVien> listEmp = ent.NhanViens.ToList();
                    List<NguoiDung> listAcc = ent.NguoiDungs.ToList();

                    foreach (NhanVien nv in listEmp)
                    {
                        if (!listAcc.Exists(n => n.NhanVienID == nv.NhanVienID))
                        {
                            ListEmployee.Add(CreateEmployeeDTOFromEntity(nv));
                        }
                    }

                    return ListEmployee;
                }

            }
            catch
            {
                return null;
            }
        }
    }
}
